package com.max.utils;

import com.max.common.LoginUserInfo;
import com.max.exceptions.BusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class SecurityUtils {

    public Long getUserId() {
        SecurityContext context = SecurityContextHolder.getContext();
        Object principal = context.getAuthentication().getPrincipal();
        if (!(principal instanceof LoginUserInfo)) {
            throw new BusinessException(HttpStatus.UNAUTHORIZED.value(), "用户未授权");
        }
        return ((LoginUserInfo) principal).getUserId();
    }


}
